package com.project.dao.impl;
import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class IUserDaoImpl extends BaseDao implements IUserDao {



    private UserBean user = null;

    @Override
    public UserBean Login(String name, String pwd) {
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from  t_user where u_name=? and u_password=? ");
            ps.setObject(1, name);
            ps.setObject(2, pwd);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = vc();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return user;
    }

    @Override
    public void add(UserBean user) {
        setConnection();
        try {
            ps = con.prepareStatement("insert into  t_user(u_name,u_password,u_birthday) values (?,?,?)");
            ps.setObject(1, user.getName());
            ps.setObject(2, user.getPwd());
            ps.setObject(3, user.getBirthday());

            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }

    }

    @Override
    public void del(int id) {
        setConnection();
        try {
            ps = con.prepareStatement("delete from t_user where pk_userId=? ");
            ps.setObject(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }

    }

    @Override
    public void update(int id, String pwd) {
        setConnection();
        try {
            ps = con.prepareStatement("update t_user set u_password=? where pk_userId=? ");
            ps.setObject(1, pwd);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public List<UserBean> findAll() {
      List<UserBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from  t_user");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(vc());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return list;
    }

    @Override
    public UserBean findById(int id) {

        this.setConnection();
        try {
            ps = con.prepareStatement("select * from  t_user where pk_userId=? ");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = vc();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return user;
    }


    public UserBean vc() throws SQLException {
        UserBean userBean = new UserBean();
        userBean.setId(rs.getInt("pk_userId"));
        userBean.setName(rs.getString("u_name"));
        userBean.setPwd(rs.getString("u_password"));
        userBean.setBirthday(LocalDate.parse(rs.getString("u_birthday")));

        return userBean;
    }

}
